Cursor help  Needed
Hi I have a department table .It is having departmentkey PK.
I need to generate following query for all departmentkey  
so need  help with cursor to update

------------------------------------------------------------------

declare @deptkey int =5
declare @monthyear date


IF OBJECT_ID('tempdb..#temp1') IS NOT NULL
    DROP TABLE  #temp1

select * into #temp1 from(
select
 convert(varchar(7), ft.closingdate, 120) as MonthYear1
,LEFT(DATENAME(MONTH, ft.closingdate), 3) + ' ' + DATENAME(YEAR, ft.closingdate) as MonthYear
,count(1) as cnt from dbo.deptmast ft  with (nolock) inner join
dbo.graphicdept dm  with (nolock) on ft.graphicKey =dmgraphicKey
where ft.deptKey =@deptkey  and dm.deptCategory ='x'
group by convert(varchar(7), ft.closingdate, 120) ,LEFT(DATENAME(MONTH, ft.closingdate), 3) + ' ' + DATENAME(YEAR, ft.closingdate)

)as t

select top 1 @monthyear=dateadd(day,1,EOMONTH((cast(monthyear AS DATE))))  from #temp1 where cnt>=15
 order by monthyear1 desc



update dbo.closingdate set winddowndate =@monthyear
where deptkey=@deptkey
February 10th, 2015 1:23am

I did myself.

thanks

  • Marked as answer by Aminesh 4 hours 50 minutes ago
Free Windows Admin Tool Kit Click here and download it now
February 10th, 2015 1:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics